package com.cuit.zuke.system.controller;

import com.cuit.zuke.common.util.Result;
import com.cuit.zuke.system.entity.SysNoticeEntity;
import com.cuit.zuke.system.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @program: zuke
 * @description: 系统公告控制器
 * @author: KamTang
 * @create: 2022-04-23 13:34
 **/
@RestController
@RequestMapping("/sys/notice")
public class SysNoticeController {

    @Autowired
    private SysNoticeService noticeService;

    /**
     * 分页展示公告
     * @param params
     * @return
     */
    @GetMapping("/list")
    public Result list (@RequestParam Map<String, Object> params) {
        return Result.ok().put("page", noticeService.queryPageBySql(params));
    }

    /**
     * 添加公告
     * @param notice
     * @return
     */
    @PostMapping("/save")
    public Result save (@RequestBody SysNoticeEntity notice) {
        boolean flag = noticeService.save(notice);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public Result deleteBatch(@RequestBody Long[] ids) {
        boolean flag = noticeService.removeByIds(Arrays.asList(ids));
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 更新
     * @param notice
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody SysNoticeEntity notice) {
        boolean flag = noticeService.updateById(notice);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}
